package com.byl.qrobot.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import android.os.CountDownTimer;
import android.widget.Toast;

import com.byl.qrobot.R;
import com.byl.qrobot.util.HttpUtil;
import com.byl.qrobot.util.LogUtil;
import com.byl.qrobot.util.SysUtils;

import net.tsz.afinal.http.AjaxParams;

public class RegisterActivity extends Activity    implements View.OnClickListener {

    private TimeCount time;
    private TextView tologin;
    private Button getcode,toregister;
    private EditText email,code,password;
    public String emailcode="";

    //提示框
    public ProgressDialog dialog;
    //服务器返回的数据
    public String infoString,ishave;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_register);
        time = new TimeCount(60000, 1000);

        initView();
    }


    private void initView() {
        tologin = (TextView) findViewById(R.id.tologin);
        getcode = (Button) findViewById(R.id.getcode);
        toregister = (Button) findViewById(R.id.toregister);

        email = (EditText) findViewById(R.id.email);
        code = (EditText) findViewById(R.id.code);
        password = (EditText) findViewById(R.id.password);

        tologin.setOnClickListener(this);
        getcode.setOnClickListener(this);
        toregister.setOnClickListener(this);
//        email.setOnClickListener(this);
//        code.setOnClickListener(this);
//        password.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.tologin:
                LogUtil.e("---------tologin----");
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.getcode:
                LogUtil.e("---------getcode----");

                //设置子线程，分别进行Get和Post传输数据
                if( email.getText().toString()!=null && email.getText().toString().indexOf("@")<0 ){
                    Toast.makeText(RegisterActivity.this,"请输入正确的邮箱！", Toast.LENGTH_SHORT).show();
                }else {

                    //togetuser_front();
                    new Thread(new togetuser()).start();
                    try {
                        Thread.sleep(1000*2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    togetcode();
                }

                break;
            case R.id.toregister:
                LogUtil.e("---------toregister----");
                //设置提示框
                if( email.getText().toString()!=null && email.getText().toString().indexOf("@")<0 ){
                    Toast.makeText(RegisterActivity.this,"请输入正确的邮箱！", Toast.LENGTH_SHORT).show();
                } else if( password.getText().toString()!=null && password.getText().toString().length()<8 ){
                    Toast.makeText(RegisterActivity.this,"密码长度至少8位！", Toast.LENGTH_SHORT).show();
                }else {
                    dialog = new ProgressDialog(RegisterActivity.this);
                    dialog.setTitle("正在注册");
                    dialog.setMessage("请稍后");
                    dialog.setCancelable(false);//设置可以通过back键取消
                    dialog.show();
                    checkmsg();
                }
                break;

        }
    }
    public void togetuser_front(){

    }
    //用户是否存在
    public class togetuser implements Runnable {
        @Override
        public void run() {
            ishave = HttpUtil.httpget("getuser?email=" + email.getText().toString() );
        }
    }
    public void togetcode(){
        //用户是否存在
        if(ishave!=null && ishave.equals("\"ok\"")){
            Toast.makeText(RegisterActivity.this,"此邮箱已注册！", Toast.LENGTH_SHORT).show();
        }else{
            new Thread(new TogetCodeThread()).start();
        }
    }
    public class TogetCodeThread implements Runnable {
        @Override
        public void run() {
            time.start();
            //new Thread(new TogetCodeThread()).start();
            emailcode = HttpUtil.httpget("getcode?email=" + email.getText().toString() );

        }
    }
    public void checkmsg(){
        String s_code = code.getText().toString();
        if(emailcode!=null && s_code!=null  && emailcode.equals("\""+s_code+"\"")){
            //验证码正确
            //发送服务器
            //设置子线程，分别进行Get和Post传输数据
            new Thread(new ToRegisterThread()).start();
            //end
        }else{
            dialog.dismiss();
            Toast.makeText(RegisterActivity.this,"验证码不正确！", Toast.LENGTH_SHORT).show();
        }

    }
    public class ToRegisterThread implements Runnable {
        @Override
        public void run() {
                infoString = HttpUtil.httpget("adduser?email=" + email.getText().toString()+"&password="+ password.getText().toString());
                showResponse(infoString);

        }
    }
    public void showResponse(final String response){
        runOnUiThread(new Runnable() {
            //更新UI
            @Override
            public void run() {
                dialog.dismiss();
                if(infoString!=null && !infoString.equals("")  && infoString.equals("\"ok\"")){
                    //跳转
                    Toast.makeText(RegisterActivity.this,"注册成功！", Toast.LENGTH_SHORT).show();
                    SysUtils.startActivity(RegisterActivity.this, LoginActivity.class);

                }else{
                    Toast.makeText(RegisterActivity.this,"注册失败！", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    //获取验证码60秒失效
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getcode.setBackgroundColor(Color.parseColor("#B6B6D8"));
            getcode.setClickable(false);
            getcode.setText("("+millisUntilFinished / 1000 +") 秒后可重新发送");
        }

        @Override
        public void onFinish() {
            getcode.setText("重新获取验证码");
            getcode.setClickable(true);
            getcode.setBackgroundColor(Color.parseColor("#4EB84A"));

        }
    }

}
